package com.ty.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 为了保证整个页面不出现乱码，使用过滤器设置全局的编码方式
 * 就不用在每一个servlet中去定义自己的编码方式。
 * @author 田野
 */
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest, servletResponse);
        // 然后去web.xml中配置一下过滤器
    }

    @Override
    public void destroy() {

    }
}
